/**
 * 
 */
package ca.uwindsor.cs.deepweb.estimation;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;


/**
 * Because we are using the dictionary from a Chinese IME, and in the experiment
 * only single word is needed, the RandomWordChooser inherits RandomWordSelector
 * which could get Chinese words from dictionary.
 * 
 * @author Liang Jie
 */
public class RandomWordChooser extends RandomWordSelector {

	/**
	 * Store used words from the dictionary
	 */
	protected Set<String> set_UsedWord;

	/**
	 * Constructor, initialize the objects
	 * 
	 * @param path
	 *            The path of the dictionary
	 * @param encoding
	 *            The encoding of the dictionary
	 * @throws IOException 
	 */
	public RandomWordChooser(String path, String encoding) throws IOException {
		super(path, encoding);
		set_UsedWord = new HashSet<String>();
	}

	/**
	 * Return a random word from the dictionary. All are different.
	 * 
	 * @return The random word
	 */
	public String getRandomCharacter() {
		int int_arraylist_words_size = list_words.size();
		int r = random.nextInt(int_arraylist_words_size);
		String string_words = new String();
		string_words = list_words.get(r);

		char[] char_words = string_words.toCharArray();
		r = random.nextInt(char_words.length);
		char char_character = char_words[r];

		while (set_UsedWord.contains(String.valueOf(char_character))) {
			r = random.nextInt(int_arraylist_words_size);
			string_words = list_words.get(r);
			char_words = string_words.toCharArray();
			random.nextInt(char_words.length);
			// Random r = new Random();
			// random = r.nextInt(char_words.length);
			char_character = char_words[r];
		}
		// System.out.println(char_character);
		return String.valueOf(char_character);
	}

	public String getRandomPhrase() {
		int r = random.nextInt(list_words.size());
		String word = new String();
		while (set_index.contains(r)) {
			r = random.nextInt(list_words.size());
		}
		set_index.add(r);
		word = list_words.get(r);
		char[] chars = word.toCharArray();
		word = String.valueOf(chars[0]);
		for (int i = 1; i < chars.length; i++) {
			word = word + " AND " + chars[i];
		}
		return word.trim();
	}

}
